﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Models
{
    public class UsersInterface
    {
        MembershipEntities membershipDB = new MembershipEntities();

        public UsersViewModel GetUserList()
        {
            List<User> users = new List<User>();

            foreach (aspnet_Users user in GetUsers())
            {
                List<string> userRoles = new List<string>();
                foreach (aspnet_Roles role in user.aspnet_Roles)
                    userRoles.Add(role.RoleName);

                users.Add(new User
                {
                    Username = user.UserName,
                    UserID = user.UserId,
                    Email = user.aspnet_Membership.Email,
                    UserRoles = userRoles.ToList()
                });
                userRoles.Clear();
            }

            List<Role> roles = new List<Role>();

            foreach(aspnet_Roles role in GetRoles())
                roles.Add(new Role
                {
                    RoleName = role.RoleName,
                    RoleID = role.RoleId,
                    Description = role.Description
                });

            return new UsersViewModel
            {
                Users = users,
                Roles = roles
            };
        }

        public IEnumerable<aspnet_Users> GetUsers()
        {
            return from userEntity in membershipDB.aspnet_Users
                   orderby userEntity.UserName
                   select userEntity;
        }

        public aspnet_Users GetUser(Guid userID)
        {
            var userQuery = from userEntity in membershipDB.aspnet_Users
                            where userEntity.UserId.Equals(userID)
                            select userEntity;

            return userQuery.FirstOrDefault();
        }

        public IEnumerable<aspnet_Roles> GetRoles()
        {
            return from roleEntity in membershipDB.aspnet_Roles
                   orderby roleEntity.RoleName
                   select roleEntity;
        }

        public aspnet_Roles GetRole(Guid roleID)
        {
            var roleQuery = from roleEntity in membershipDB.aspnet_Roles
                            where roleEntity.RoleId.Equals(roleID)
                            select roleEntity;

            return roleQuery.FirstOrDefault();
        }

        public string ResetUserPassword(aspnet_Users user)
        {
            MembershipProvider provider = Membership.Provider;
            MembershipUser currentUser = provider.GetUser(user.UserName, false);
            return currentUser.ResetPassword();
        }

        public void AddUser(aspnet_Users user)
        {
            membershipDB.aspnet_Users.AddObject(user);
        }

        public void DeleteUser(aspnet_Users user)
        {
            membershipDB.aspnet_Users.DeleteObject(user);
        }

        public void Save()
        {
            membershipDB.SaveChanges();
        }
    }

}